package server;

import java.net.*;
import java.io.*;

public class Server
{
	public static void main(String[] args) throws IOException
	{
		ServerSocket serverSocket = null;
		boolean listening = true;
		int port = 4444;

		try {
			serverSocket = new ServerSocket(port);
			
			// print out the server address
			InetAddress i = InetAddress.getLocalHost();
			
			System.out.println("Server host name: " + i.getHostName());
			System.out.println("Server host IP: " + i.getHostAddress());
			System.out.println("Listening for connections on port " + port);
		} catch (IOException e) {
			System.err.println("Could not listen on port " + port);
			System.exit(-1);
		}

		while (listening) // listen for connections
		{
			// accept a new connection
			Socket s = serverSocket.accept();
			s.setKeepAlive(true);
			ServerThread clientConnection = new ServerThread(s);
			
			clientConnection.start();
			
			//new ServerThread(serverSocket.accept()).start();
			System.out.println("Connection accepted");
		}

		System.out.println("Server socket closed");
		serverSocket.close();
	}
}
